]> git.r.bdr.sh - rbdr/super-polarity/blame - Super Polarity/Actors/MainShip.cs
Stuffffffff
[rbdr/super-polarity] / Super Polarity / Actors / MainShip.cs
CommitLineData
2af83e98
BB
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
38c7d3f9 5using System.Threading;
2af83e98
BB
6using Microsoft.Xna.Framework;
7using Microsoft.Xna.Framework.Content;
8using Microsoft.Xna.Framework.Graphics;
9
10namespace SuperPolarity
11{
12 class MainShip : Ship
13 {
38c7d3f9
BB
14
15 public static Color BlueColor;
16 public static Color RedColor;
17
2af83e98
BB
18 ParticleEngine particleEngine;
19
38c7d3f9
BB
20 protected bool Shooting;
21 protected int ShotCooldown;
22
74c15570
BB
23 protected float CurrentImmortalTime;
24 protected float MaxImmortalTime;
25 protected bool Flashing;
26
27 public MainShip(SuperPolarity newGame) : base(newGame) {}
2af83e98 28
38c7d3f9
BB
29 ~MainShip()
30 {
31 particleEngine = null;
32 }
33
2af83e98
BB
34 public override void Initialize(Texture2D texture, Vector2 position)
35 {
36 base.Initialize(texture, position);
37
38c7d3f9
BB
38 MainShip.BlueColor = new Color(230, 244, 249);
39 MainShip.RedColor = new Color(255, 234, 241);
40
2af83e98
BB
41 InitParticleEngine();
42 SetPolarity(Polarity.Positive);
43
38c7d3f9 44 ShotCooldown = 50;
74c15570
BB
45 MaxImmortalTime = 1500;
46
47 BoxDimensions.X = 2;
48 BoxDimensions.Y = 2;
49 BoxDimensions.W = 2;
50 BoxDimensions.Z = 2;
51 InitBox();
2af83e98
BB
52
53 BindInput();
54 }
55
56 void InitParticleEngine()
57 {
38c7d3f9 58 particleEngine = ParticleEffectFactory.CreatePolarCircle(Position);
2af83e98
BB
59 }
60
61 void BindInput()
62 {
63 InputController.Bind("moveX", HandleHorizontalMovement);
64 InputController.Bind("moveY", HandleVerticalMovement);
65 InputController.Bind("changePolarity", HandleChangePolarity);
38c7d3f9
BB
66 InputController.Bind("shoot", HandleShot);
67 }
68
69 protected void HandleShot(float value)
70 {
74c15570
BB
71 var bullet = ActorFactory.CreateBullet(Position, Angle);
72
73 Children.Add(bullet);
74 bullet.Parent = this;
75
38c7d3f9
BB
76 Shooting = true;
77 Timer t = new Timer(new TimerCallback(UnlockShot));
78 t.Change(ShotCooldown, Timeout.Infinite);
79 }
80
81 protected void UnlockShot(object state)
82 {
83 InputController.Unlock("shoot");
2af83e98
BB
84 }
85
86 protected void HandleChangePolarity(float value)
87 {
88 SwitchPolarity();
89 }
90
91 public void HandleHorizontalMovement(float value)
92 {
93 Acceleration.X = value * AccelerationRate;
94
95 if (value > 0.1 && Velocity.X < 0 || value < 0.1 && Velocity.X > 0)
96 {
97 Acceleration.X *= 2;
98 }
99
100 if (value > 0.1 && Velocity.Y < 0 || value < 0.1 && Velocity.Y > 0)
101 {
102 Acceleration.Y *= 2;
103 }
104 }
105
106 public void HandleVerticalMovement(float value)
107 {
108 Acceleration.Y = value * AccelerationRate;
109 }
110
111 public override void SwitchPolarity()
112 {
113 base.SwitchPolarity();
114 SwitchParticleEngine(CurrentPolarity);
74c15570 115 game.Player.ResetMultiplier();
2af83e98
BB
116 }
117
118 public override void SetPolarity(Polarity newPolarity)
119 {
120 base.SetPolarity(newPolarity);
121 SwitchParticleEngine(newPolarity);
122 }
123
124 protected void SwitchParticleEngine(Polarity polarity)
125 {
126 if (polarity == Polarity.Positive)
127 {
38c7d3f9 128 particleEngine.Color = MainShip.RedColor;
2af83e98
BB
129 }
130 else if (polarity == Polarity.Negative)
131 {
38c7d3f9 132 particleEngine.Color = MainShip.BlueColor;
2af83e98
BB
133 }
134 else
135 {
136 particleEngine.Color = Color.Gray;
137 }
138 }
139
140 public override void Update(GameTime gameTime)
141 {
142 base.Update(gameTime);
143 particleEngine.EmitterLocation = Position;
144 particleEngine.Update();
145 ConstrainToEdges();
74c15570 146 UpdateImmortality(gameTime);
38c7d3f9
BB
147 Shooting = false;
148 }
149
74c15570
BB
150 public void UpdateImmortality(GameTime gameTime)
151 {
152 if (Immortal)
153 {
154 CurrentImmortalTime += gameTime.ElapsedGameTime.Milliseconds;
155
156 if (Flashing)
157 {
158 Color = new Color(255, 255, 255, 128);
159 }
160 else
161 {
162 Color = Color.White;
163 }
164
165 Flashing = !Flashing;
166
167 if (CurrentImmortalTime > MaxImmortalTime)
168 {
169 Immortal = false;
170 Color = Color.White;
171 }
172 }
173 }
174
38c7d3f9
BB
175 public override void Move(GameTime gameTime)
176 {
177 base.Move(gameTime);
178
179 if (Shooting)
180 {
181 if (Velocity.X > ActVelocity)
182 {
183 Velocity.X = ActVelocity;
184 }
185
186 if (Velocity.X < -ActVelocity)
187 {
188 Velocity.X = -ActVelocity;
189 }
190
191 if (Velocity.Y > ActVelocity)
192 {
193 Velocity.Y = ActVelocity;
194 }
195
196 if (Velocity.Y < -ActVelocity)
197 {
198 Velocity.Y = -ActVelocity;
199 }
200 }
2af83e98
BB
201 }
202
203 public override void Magnetize(Ship ship, float distance, float angle)
204 {
205 }
206
207 protected void ConstrainToEdges()
208 {
209 if (Position.X < 0)
210 {
211 Position.X = 0;
212
213 if (Velocity.X < 0)
214 {
215 Velocity.X = 0;
216 }
217 }
218 if (Position.X > game.GraphicsDevice.Viewport.Width)
219 {
220 Position.X = game.GraphicsDevice.Viewport.Width;
221
222 if (Velocity.X > 0)
223 {
224 Velocity.X = 0;
225 }
226 }
227 if (Position.Y < 0)
228 {
229 Position.Y = 0;
230
231 if (Velocity.Y < 0)
232 {
233 Velocity.Y = 0;
234 }
235 }
236 if (Position.Y > game.GraphicsDevice.Viewport.Height)
237 {
238 Position.Y = game.GraphicsDevice.Viewport.Height;
239
240 if (Velocity.Y < 0)
241 {
242 Velocity.Y = 0;
243 }
244 }
245 }
246
247 public override void Draw(SpriteBatch spriteBatch)
248 {
249 particleEngine.Draw(spriteBatch);
250 base.Draw(spriteBatch);
251 }
74c15570
BB
252
253 public override void Collide(Actor other, Rectangle collision)
254 {
255 if (other.GetType().IsAssignableFrom(typeof(StandardShip)) &&
256 !Immortal)
257 {
258 Die();
259 }
260 }
261
262 protected override void Die()
263 {
264 game.Player.Lives = game.Player.Lives - 1;
265 game.Player.ResetMultiplier();
266 if (game.Player.Lives < 0)
267 {
268 Dying = true;
269 }
270 else {
271 Immortal = true;
272 CurrentImmortalTime = 0;
273 }
274 }
2af83e98
BB
275 }
276}